/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package acousticfield3d.gui.panels;

import acousticfield3d.algorithms.CalcField;
import acousticfield3d.gui.MainForm;
import acousticfield3d.math.Vector2f;
import acousticfield3d.math.Vector3f;
import acousticfield3d.scene.Entity;
import acousticfield3d.scene.Scene;
import acousticfield3d.simulation.Transducer;
import acousticfield3d.simulation.Trap;
import acousticfield3d.utils.Parse;
import java.util.List;

/**
 *
 * @author am14010
 */
public class TrapsPanel extends javax.swing.JPanel {
    final MainForm mf;
            
    public TrapsPanel(MainForm mf) {
        this.mf = mf;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        focusCheck = new javax.swing.JCheckBox();
        twinRadio = new javax.swing.JRadioButton();
        angleText = new javax.swing.JTextField();
        vortexRadio = new javax.swing.JRadioButton();
        mText = new javax.swing.JTextField();
        noneRadio = new javax.swing.JRadioButton();
        calcButton = new javax.swing.JButton();
        calcClickCheck = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        pressureAText = new javax.swing.JTextField();
        pressureBText = new javax.swing.JTextField();
        pressureDivText = new javax.swing.JTextField();
        calcForceCheck = new javax.swing.JCheckBox();
        xForceText = new javax.swing.JTextField();
        yForceText = new javax.swing.JTextField();
        zForceText = new javax.swing.JTextField();
        calcLaplacianCheck = new javax.swing.JCheckBox();
        sendCheck = new javax.swing.JCheckBox();
        calcDiscTmpButton = new javax.swing.JButton();

        focusCheck.setSelected(true);
        focusCheck.setText("focus");

        buttonGroup1.add(twinRadio);
        twinRadio.setSelected(true);
        twinRadio.setText("twin");

        angleText.setText("90");

        buttonGroup1.add(vortexRadio);
        vortexRadio.setText("vortex");

        mText.setText("1");

        buttonGroup1.add(noneRadio);
        noneRadio.setText("none");

        calcButton.setText("Calc");
        calcButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcButtonActionPerformed(evt);
            }
        });

        calcClickCheck.setText("calc");

        jLabel3.setText("Pressure");

        pressureAText.setText("500");

        pressureBText.setText("1500");

        pressureDivText.setEditable(false);
        pressureDivText.setText("3");

        calcForceCheck.setText("calc force");

        xForceText.setText("1500");

        yForceText.setText("1500");

        zForceText.setText("1500");

        calcLaplacianCheck.setText("calc lap");

        sendCheck.setText("send");

        calcDiscTmpButton.setText("T");
        calcDiscTmpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcDiscTmpButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(vortexRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mText))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(noneRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(twinRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(angleText))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pressureAText))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pressureDivText)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pressureBText))
                    .addComponent(xForceText)
                    .addComponent(yForceText)
                    .addComponent(zForceText)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(calcClickCheck)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sendCheck))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(calcForceCheck)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(calcLaplacianCheck)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(focusCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(calcButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(calcDiscTmpButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(focusCheck)
                    .addComponent(calcButton)
                    .addComponent(calcDiscTmpButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(noneRadio)
                    .addComponent(twinRadio)
                    .addComponent(angleText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vortexRadio)
                    .addComponent(mText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(calcClickCheck)
                    .addComponent(sendCheck))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(pressureAText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pressureDivText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pressureBText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(calcForceCheck)
                    .addComponent(calcLaplacianCheck))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(xForceText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(yForceText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(zForceText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    
    public void applyOnTarget(final Vector3f target){
        final List<Transducer> transducers = mf.simulation.getTransducers();
        getTrap().apply(mf.simulation, transducers, target);
    }
    
    private void calcButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calcButtonActionPerformed
        Entity cp = Scene.getFirstWithTag( mf.getSelection(), Entity.TAG_CONTROL_POINT );
        
        if (cp == null){
            return;
        }
        
        applyOnTarget( cp.getTransform().getTranslation() );
        
        mf.needUpdate();
    }//GEN-LAST:event_calcButtonActionPerformed

    private void calcDiscTmpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calcDiscTmpButtonActionPerformed
        //get the selected particle
        final Entity e = mf.getSelection().get(0);
        final Vector3f pos = e.getTransform().getTranslation();
        
        for(int i = 32; i >=0; --i){
            //set the phase discretiation
            mf.miscPanel.getPhaseDiscreCheck().setSelected( i != 0 );
            if (i != 0){
                mf.miscPanel.getPhaseDiscreText().setText(i + "");
            }
            
            //click calcat
            clickAt(pos);
            
            //print values
            System.out.println(i + " " + pressureAText.getText() + " " + xForceText.getText() + " " + yForceText.getText() + " " + zForceText.getText());
        }
    }//GEN-LAST:event_calcDiscTmpButtonActionPerformed


    public boolean isCalcOnClick(){
        return calcClickCheck.isSelected();
    }
    
    public void setPressureA(float p){
        pressureAText.setText( p + "");
        updatePressureDiv();
    }
    
    private void updatePressureDiv(){
        final float pA = Parse.toFloat( pressureAText.getText() );
        final float pB = Parse.toFloat( pressureBText.getText() );
        pressureDivText.setText( (pA/pB) + "");
    }
    
    public Trap getTrap(){
        final Trap t = new Trap();
        
        t.focus = focusCheck.isSelected();
        if (twinRadio.isSelected() ) {
            t.type = Trap.TrapType.Twin;
            t.parameter1 = Parse.toFloat( angleText.getText() );
        }else if (vortexRadio.isSelected() ) {
            t.type = Trap.TrapType.Vortex;
            t.parameter1 = Parse.toFloat( mText.getText() );
        }
        return t;
    }

    public void clickAt(Vector3f wPos) {
        final Vector2f field = CalcField.calcFieldAt(wPos, mf);
        setPressureA(field.length());

        if (calcClickCheck.isSelected() ) {
            applyOnTarget(wPos);
            if ( sendCheck.isSelected() ){
                mf.transControlPanel.sendPattern();
            }
            mf.needUpdate();
        }
        
        if (calcForceCheck.isSelected() || calcLaplacianCheck.isSelected()){
            
            Entity bead = mf.scene.getFirstWithTag( Entity.TAG_CONTROL_POINT | Entity.TAG_BEAD);
            final Vector3f pos = bead.getTransform().getTranslation();
            
            final float particleR = bead != null ? bead.getTransform().getScale().maxComponent() / 2.0f: 0.0005f;
          
            Vector3f v3 = Vector3f.ZERO;
            if ( calcForceCheck.isSelected() ){
               v3 = CalcField.calcForceAt(pos.x, pos.y, pos.z, particleR, mf);

            }else if ( calcLaplacianCheck.isSelected() ){
               v3 = CalcField.calcForceGradients(pos.x, pos.y, pos.z, particleR, mf);
            }
            
            xForceText.setText( v3.x + "");
            yForceText.setText( v3.y + "");
            zForceText.setText( v3.z + "");
        }
        
       
    }
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField angleText;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton calcButton;
    private javax.swing.JCheckBox calcClickCheck;
    private javax.swing.JButton calcDiscTmpButton;
    private javax.swing.JCheckBox calcForceCheck;
    private javax.swing.JCheckBox calcLaplacianCheck;
    private javax.swing.JCheckBox focusCheck;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField mText;
    private javax.swing.JRadioButton noneRadio;
    private javax.swing.JTextField pressureAText;
    private javax.swing.JTextField pressureBText;
    private javax.swing.JTextField pressureDivText;
    private javax.swing.JCheckBox sendCheck;
    private javax.swing.JRadioButton twinRadio;
    private javax.swing.JRadioButton vortexRadio;
    private javax.swing.JTextField xForceText;
    private javax.swing.JTextField yForceText;
    private javax.swing.JTextField zForceText;
    // End of variables declaration//GEN-END:variables


}
